package coreservlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SubmitResume extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		if (req.getParameter("previewButton") != null) {
			showPreview(req, out);
		} else {
			storeResume(req);
			showConfirmation(req, out);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	private void showPreview(HttpServletRequest request, PrintWriter out) {
		String headingFont = request.getParameter("headingFont");
		headingFont = replaceIfMissingOrDefault(headingFont, "");
		int headingSize = getSize(request.getParameter("headingSize"), 32);
		String bodyFont = request.getParameter("bodyFont");
		bodyFont = replaceIfMissingOrDefault(bodyFont, "");
		int bodySize = getSize(request.getParameter("bodySize"), 18);
		String fgColor = request.getParameter("fgColor");
		fgColor = replaceIfMissing(fgColor, "BLACK");
		String bgColor = request.getParameter("bgColor");
		bgColor = replaceIfMissing(bgColor, "WHITE");
		String name = request.getParameter("name");
		name = replaceIfMissing(name, "Lou Zer");
		String title = request.getParameter("title");
		title = replaceIfMissing(title, "Loser");
		String email = request.getParameter("email");
		email = replaceIfMissing(email, "contact@hot-computer-jobs.com");
		String languages = request.getParameter("languages");
		languages = replaceIfMissing(languages, "<I>None</I>");
		String languageList = makeList(languages);
		String skills = request.getParameter("skills");
		skills = replaceIfMissing(skills, "Not many, obviously.");
		out.println(ServletUtilities.DOCTYPE
				+ "\n"
				+ "<HTML><HEAD><TITLE>Resume for "
				+ name
				+ "</TITLE>\n"
				+ makeStyleSheet(headingFont, headingSize, bodyFont, bodySize,
						fgColor, bgColor) + "\n" + "</HEAD>\n" + "<BODY>\n"
				+ "<CENTER>\n" + "<SPAN CLASS=\"HEADING1\">" + name
				+ "</SPAN><BR>\n" + "<SPAN CLASS=\"HEADING2\">" + title
				+ "<BR>\n" + "<A HREF=\"mailto:" + email + "\">" + email
				+ "</A></SPAN>\n" + "</CENTER><BR><BR>\n"
				+ "<SPAN CLASS=\"HEADING3\">Programming Languages"
				+ "</SPAN>\n" + makeList(languages) + "<BR><BR>\n"
				+ "<SPAN CLASS=\"HEADING3\">Skills and Experience"
				+ "</SPAN><BR><BR>\n" + skills + "\n" + "</BODY></HTML>");
	}

	private String makeStyleSheet(String headingFont, int heading1Size,
			String bodyFont, int bodySize, String fgColor, String bgColor) {
		int heading2Size = heading1Size * 7 / 10;
		int heading3Size = heading1Size * 6 / 10;
		String styleSheet = "<STYLE TYPE=\"text/css\">\n" + "<!--\n"
				+ ".HEADING1 { font-size: "
				+ heading1Size
				+ "px;\n"
				+ " font-weight: bold;\n"
				+ " font-family: "
				+ headingFont
				+ "Arial, Helvetica, sans-serif;\n"
				+ "}\n"
				+ ".HEADING2 { font-size: "
				+ heading2Size
				+ "px;\n"
				+ " font-weight: bold;\n"
				+ " font-family: "
				+ headingFont
				+ "Arial, Helvetica, sans-serif;\n"
				+ "}\n"
				+ ".HEADING3 { font-size: "
				+ heading3Size
				+ "px;\n"
				+ " font-weight: bold;\n"
				+ " font-family: "
				+ headingFont
				+ "Arial, Helvetica, sans-serif;\n"
				+ "}\n"
				+ "BODY { color: "
				+ fgColor
				+ ";\n"
				+ " background-color: "
				+ bgColor
				+ ";\n"
				+ " font-size: "
				+ bodySize
				+ "px;\n"
				+ " font-family: "
				+ bodyFont
				+ "Times New Roman, Times, serif;\n"
				+ "}\n"
				+ "A:hover { color: red; }\n" + "-->\n" + "</STYLE>";
		return (styleSheet);
	}

	private String makeList(String listItems) {
		StringTokenizer tokenizer = new StringTokenizer(listItems, ", ");
		String list = "<UL>\n";
		while (tokenizer.hasMoreTokens()) {
			list = list + " <LI>" + tokenizer.nextToken() + "\n";
		}
		list = list + "</UL>";
		return (list);
	}

	private String replaceIfMissing(String orig, String replacement) {
		if ((orig == null) || (orig.trim().equals(""))) {
			return (replacement);
		} else {
			return (orig);
		}
	}

	private String replaceIfMissingOrDefault(String orig, String replacement) {
		if ((orig == null) || (orig.trim().equals(""))
				|| (orig.equals("default"))) {
			return (replacement);
		} else {
			return (orig + ", ");
		}
	}

	private int getSize(String sizeString, int defaultSize) {
		try {
			return (Integer.parseInt(sizeString));
		} catch (NumberFormatException nfe) {
			return (defaultSize);
		}
	}

	private void showConfirmation(HttpServletRequest request, PrintWriter out) {
		String title = "Submission Confirmed.";
		out.println(ServletUtilities.headWithTitle(title) + "<BODY>\n" + "<H1>"
				+ title + "</H1>\n"
				+ "Your resume should appear online within\n"
				+ "24 hours. If it doesn't, try submitting\n"
				+ "again with a different email address.\n" + "</BODY></HTML>");
	}

	private void storeResume(HttpServletRequest request) {
		String email = request.getParameter("email");
		putInSpamList(email);
	}

	private void putInSpamList(String emailAddress) {
		// Code removed to protect the guilty.
	}
}
